package com.enhinck.dbutilweb.common.freemark.mybatisplus;

import com.enhinck.dbutilweb.common.excel.JavaDefineEntity;
import com.enhinck.dbutilweb.common.excel.JavaFieldEntity;
import com.enhinck.dbutilweb.common.freemark.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * feign client
 *
 * @author huenbin
 * @date 2020-07-02 17:00
 */
public class RpcInterfaceFactory extends BaseFacotry {
    private static final RpcInterfaceFactory doFactory = new RpcInterfaceFactory();

    public static RpcInterfaceFactory getInstance() {
        return doFactory;
    }


    private RpcInterfaceFactory() {

    }


    public JavaClassEntity create(JavaDefineEntity<JavaFieldEntity> javaDefineEntity) {
        JavaClassEntity javaClassEntity = createInteface(javaDefineEntity);
        javaClassEntity.setRpc(true);
        javaClassEntity.setInterface(true);
        String className = getFeignName(javaDefineEntity);
        javaClassEntity.setClassName(className);



        List<String> annotations = new ArrayList<>();
        annotations.add("@FeignClient(\"\")");
        javaClassEntity.setAnnotations(annotations);

        Set<String> importList = new HashSet<>();

        importList.add("java.util.List");
        importList.add("lombok.*");
        importList.add("io.swagger.annotations.*");
        importList.add("org.springframework.web.bind.annotation.*");


        String responseWrapper = null;

        String pageWrapper = null;
        if (isIoc) {
            importList.add("com.greentown.common.response.WebResponseEntity");
            importList.add("com.baomidou.mybatisplus.extension.plugins.pagination.Page");
            responseWrapper = "WebResponseEntity";
            pageWrapper = "Page";
        } else {
            importList.add("com.greentown.phantom.utils.DataTransformer");
            importList.add("com.greentown.phantom.common.response.WebResponse");
            importList.add("com.kingboy.common.utils.page.PageResult");
            responseWrapper = "WebResponse";
            pageWrapper = "PageResult";
        }

        importList.add("org.springframework.cloud.netflix.feign.FeignClient");

        javaClassEntity.setImportList(importList);
        importList.addAll(javaDefineEntity.getFeignImports());
        javaClassEntity.setPackagePath(javaDefineEntity.getFeignPackageName());


        List<ClassMethod> classMethods = new ArrayList<>();
        javaClassEntity.setMethods(classMethods);
        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setClassMethodDescribe("列表");
            classMethod.setMethodReturnType(responseWrapper + "<List<" + javaDefineEntity.getJavaName() + "DTO>>");
            classMethod.setMethodName("list");
            List<String> methodAnnotations = new ArrayList<>();
            methodAnnotations.add("@ApiOperation(\"列表\")");
            methodAnnotations.add("@GetMapping(\"/rpc/" + javaDefineEntity.getJavaName().toLowerCase() + "s\")");
            classMethod.setAnnotations(methodAnnotations);
            classMethods.add(classMethod);
        }


        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper + "<Boolean>");
            classMethod.setMethodName("add");
            classMethod.setClassMethodDescribe("新增");
            List<String> methodAnnotations = new ArrayList<>();
            methodAnnotations.add("@ApiOperation(\"新增\")");
            methodAnnotations.add("@PostMapping(\"/rpc/" + javaDefineEntity.getJavaName().toLowerCase() + "\")");
            classMethod.setAnnotations(methodAnnotations);
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@RequestBody " + javaDefineEntity.getJavaName() + "DTO");
            methodParam.setFieldName("entityDTO");
            methodParam.setClassFieldDescribe("新增对象");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            classMethods.add(classMethod);
        }

        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper + "<Boolean>");
            classMethod.setMethodName("update");
            classMethod.setClassMethodDescribe("更新");
            List<String> methodAnnotations = new ArrayList<>();
            methodAnnotations.add("@ApiOperation(\"更新\")");
            methodAnnotations.add("@PutMapping(\"/rpc/" + javaDefineEntity.getJavaName().toLowerCase() + "\")");
            classMethod.setAnnotations(methodAnnotations);
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@RequestBody " + javaDefineEntity.getJavaName() + "DTO");
            methodParam.setFieldName("entityDTO");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            classMethods.add(classMethod);
        }

        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper + "<" + javaDefineEntity.getJavaName() + "DTO>");
            classMethod.setMethodName("get");
            classMethod.setClassMethodDescribe("查询");
            List<String> methodAnnotations = new ArrayList<>();
            methodAnnotations.add("@ApiOperation(\"查询\")");
            methodAnnotations.add("@GetMapping(\"/rpc/" + javaDefineEntity.getJavaName().toLowerCase() + "/{id}\")");
            classMethod.setAnnotations(methodAnnotations);
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@PathVariable(\"id\") Long");
            methodParam.setFieldName("id");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            classMethods.add(classMethod);
        }

        {
            ClassMethod classMethod = new ClassMethod();
            classMethod.setMethodReturnType(responseWrapper + "<Boolean>");
            classMethod.setMethodName("delete");
            classMethod.setClassMethodDescribe("删除");
            List<String> methodAnnotations = new ArrayList<>();
            methodAnnotations.add("@ApiOperation(\"删除\")");
            methodAnnotations.add("@DeleteMapping(\"/rpc/" + javaDefineEntity.getJavaName().toLowerCase() + "/{id}\")");
            classMethod.setAnnotations(methodAnnotations);
            ClassField methodParam = new ClassField();
            methodParam.setFieldType("@PathVariable(\"id\") Long");
            methodParam.setFieldName("id");
            List<ClassField> paramTypes = new ArrayList<>();
            paramTypes.add(methodParam);
            classMethod.setMethodParamTypes(paramTypes);
            classMethods.add(classMethod);
        }


        return javaClassEntity;
    }


    public void write(JavaDefineEntity<JavaFieldEntity> javaDefineEntity) {
        FreemarkUtil.write(create(javaDefineEntity));
    }


}
